﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public void MapBSSATZ(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveBSSATZ(DataRow dwData, DataRow dwDispo)
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryBSSATZ(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }

            MapBSSATZ(ref dwLocal, dwData);

            if (!flgExist)
            {
                InsertBSSATZ(dwLocal);
            }
            UpdateBSSATZ(dwLocal);
        }
        public void InsertBSSATZ(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "INSERT INTO BSSATZ(BSMANDANT, BSKART, BSAENDZL, BSAENDBEN, BSAENDDAT, BSNEUBEN, BSNEUDAT, BSMTART, BSBEZ, BSSTDKVAR, BSSTAKZ, BSMUSTER, BSFARBVW, BSFARBSTUE, BSPFVARN, BSPROGRP, BSARTGRP, BSPMGRP, BSBAUMKZ, BSVERWEN, BSFDZ, BSFDZLEIST, BSKNOTEN, BSFAKTOR, BSSRFNR, BSCADKEY, BSANZTB, BSKRANF, BSKRENDE, BSKRMITTE, BSFDFLGKR, BSKRART, BSSCHAERBR, BSRIETF, BSFDFLGRI, BSBEM, BSZOLLTNR, BSSUBSTGR, BSHANDICAP, BSAFFINITY, BSSUBST1, BSSUBST2, BSBLEACHED, BSCLORINEST, BSFIXTEMP, BSPRODH, BSMATKL, BSPLGRP, BSPLANART, BSEKGRP, BSBSTME, BSVABME, BSEKWSL, BSINKZ, BSINPR, BSBEWAB, BSFILLER)"
                + "VALUES (:BSMANDANT, :BSKART, 0, '        ', 0, :BSNEUBEN, :BSNEUDAT, '    ', '                                        ', '1 ', 'A', 'N', 'J', 'J', ' ', '   ', '   ', '    ', :BSBAUMKZ,:BSVERWEN, 0, 0, 'J', 1000, '               ', '                              ', '00', '          ', '          ', ' ', '           ', '          ', 0, 0, '           ', '                                        ', '                    ', '   ', '   ', '               ', '  ', '  ', ' ', ' ', 0, '                  ', '         ', '  ', '                    ', '   ', '   ', ' ', '    ', ' ', 0, 0, '                                                                ')";
            or1.Parameters.Add("BSMANDANT", OracleType.NVarChar).Value = dwData["BSMANDANT"].ToString();
            or1.Parameters.Add("BSKART", OracleType.NVarChar).Value = dwData["BSKART"].ToString();
            or1.Parameters.Add("BSNEUBEN", OracleType.NVarChar).Value = dwData["BSNEUBEN"].ToString();
            or1.Parameters.Add("BSNEUDAT", OracleType.Number).Value = dwData["BSNEUDAT"].ToString();
            or1.Parameters.Add("BSBAUMKZ", OracleType.NVarChar).Value = dwData["BSBAUMKZ"].ToString();
            or1.Parameters.Add("BSVERWEN", OracleType.NVarChar).Value = dwData["BSVERWEN"].ToString();
            setResult = Connect2Proxy(or1);
        }
        public void DeleteBSSATZ(string client, string art)
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "DELETE  BSSATZ"
           + " WHERE "
            + " BSMANDANT=:BSMANDANT AND BSKART=:BSKART";
            or1.Parameters.Add("BSMANDANT", OracleType.NVarChar).Value = client;
            or1.Parameters.Add("BSKART", OracleType.NVarChar).Value = art;
            Connect2Proxy(or1);
        }
        public void UpdateBSSATZ(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            or1.CommandText = "UPDATE BSSATZ SET "
                + "BSMANDANT=:BSMANDANT,"
                + "BSKART=:BSKART,"
                + "BSAENDZL=:BSAENDZL,"
                + "BSAENDBEN=:BSAENDBEN,"
                + "BSAENDDAT=:BSAENDDAT,"
                + "BSNEUBEN=:BSNEUBEN,"
                + "BSNEUDAT=:BSNEUDAT,"
                + "BSMTART=:BSMTART,"
                + "BSBEZ=:BSBEZ,"
                + "BSSTDKVAR=:BSSTDKVAR,"
                + "BSSTAKZ=:BSSTAKZ,"
                + "BSMUSTER=:BSMUSTER,"
                + "BSFARBVW=:BSFARBVW,"
                + "BSFARBSTUE=:BSFARBSTUE,"
                + "BSPFVARN=:BSPFVARN,"
                + "BSPROGRP=:BSPROGRP,"
                + "BSARTGRP=:BSARTGRP,"
                + "BSPMGRP=:BSPMGRP,"
                + "BSBAUMKZ=:BSBAUMKZ,"
                + "BSVERWEN=:BSVERWEN,"
                + "BSFDZ=:BSFDZ,"
                + "BSFDZLEIST=:BSFDZLEIST,"
                + "BSKNOTEN=:BSKNOTEN,"
                + "BSFAKTOR=:BSFAKTOR,"
                + "BSSRFNR=:BSSRFNR,"
                + "BSCADKEY=:BSCADKEY,"
                + "BSANZTB=:BSANZTB,"
                + "BSKRANF=:BSKRANF,"
                + "BSKRENDE=:BSKRENDE,"
                + "BSKRMITTE=:BSKRMITTE,"
                + "BSFDFLGKR=:BSFDFLGKR,"
                + "BSKRART=:BSKRART,"
                + "BSSCHAERBR=:BSSCHAERBR,"
                + "BSRIETF=:BSRIETF,"
                + "BSFDFLGRI=:BSFDFLGRI,"
                + "BSBEM=:BSBEM,"
                + "BSZOLLTNR=:BSZOLLTNR,"
                + "BSSUBSTGR=:BSSUBSTGR,"
                + "BSHANDICAP=:BSHANDICAP,"
                + "BSAFFINITY=:BSAFFINITY,"
                + "BSSUBST1=:BSSUBST1,"
                + "BSSUBST2=:BSSUBST2,"
                + "BSBLEACHED=:BSBLEACHED,"
                + "BSCLORINEST=:BSCLORINEST,"
                + "BSFIXTEMP=:BSFIXTEMP,"
                + "BSPRODH=:BSPRODH,"
                + "BSMATKL=:BSMATKL,"
                + "BSPLGRP=:BSPLGRP,"
                + "BSPLANART=:BSPLANART,"
                + "BSEKGRP=:BSEKGRP,"
                + "BSBSTME=:BSBSTME,"
                + "BSVABME=:BSVABME,"
                + "BSEKWSL=:BSEKWSL,"
                + "BSINKZ=:BSINKZ,"
                + "BSINPR=:BSINPR,"
                + "BSBEWAB=:BSBEWAB,"
                + "BSFILLER=:BSFILLER "
                + " WHERE  "
                + "  BSMANDANT=:BSMANDANT AND BSKART=:BSKART";

            or1.Parameters.Add("BSMANDANT", OracleType.NVarChar).Value = dwData["BSMANDANT"].ToString();
            or1.Parameters.Add("BSKART", OracleType.NVarChar).Value = dwData["BSKART"].ToString();
            or1.Parameters.Add("BSAENDZL", OracleType.NVarChar).Value = dwData["BSAENDZL"].ToString();
            or1.Parameters.Add("BSAENDBEN", OracleType.NVarChar).Value = dwData["BSAENDBEN"].ToString();
            or1.Parameters.Add("BSAENDDAT", OracleType.NVarChar).Value = dwData["BSAENDDAT"].ToString();
            or1.Parameters.Add("BSNEUBEN", OracleType.NVarChar).Value = dwData["BSNEUBEN"].ToString();
            or1.Parameters.Add("BSNEUDAT", OracleType.NVarChar).Value = dwData["BSNEUDAT"].ToString();
            or1.Parameters.Add("BSMTART", OracleType.NVarChar).Value = dwData["BSMTART"].ToString();
            or1.Parameters.Add("BSBEZ", OracleType.NVarChar).Value = dwData["BSBEZ"].ToString();
            or1.Parameters.Add("BSSTDKVAR", OracleType.NVarChar).Value = dwData["BSSTDKVAR"].ToString();
            or1.Parameters.Add("BSSTAKZ", OracleType.NVarChar).Value = dwData["BSSTAKZ"].ToString();
            or1.Parameters.Add("BSMUSTER", OracleType.NVarChar).Value = dwData["BSMUSTER"].ToString();
            or1.Parameters.Add("BSFARBVW", OracleType.NVarChar).Value = dwData["BSFARBVW"].ToString();
            or1.Parameters.Add("BSFARBSTUE", OracleType.NVarChar).Value = dwData["BSFARBSTUE"].ToString();
            or1.Parameters.Add("BSPFVARN", OracleType.NVarChar).Value = dwData["BSPFVARN"].ToString();
            or1.Parameters.Add("BSPROGRP", OracleType.NVarChar).Value = dwData["BSPROGRP"].ToString();
            or1.Parameters.Add("BSARTGRP", OracleType.NVarChar).Value = dwData["BSARTGRP"].ToString();
            or1.Parameters.Add("BSPMGRP", OracleType.NVarChar).Value = dwData["BSPMGRP"].ToString();
            or1.Parameters.Add("BSBAUMKZ", OracleType.NVarChar).Value = dwData["BSBAUMKZ"].ToString();
            or1.Parameters.Add("BSVERWEN", OracleType.NVarChar).Value = dwData["BSVERWEN"].ToString();
            or1.Parameters.Add("BSFDZ", OracleType.NVarChar).Value = dwData["BSFDZ"].ToString();
            or1.Parameters.Add("BSFDZLEIST", OracleType.NVarChar).Value = dwData["BSFDZLEIST"].ToString();
            or1.Parameters.Add("BSKNOTEN", OracleType.NVarChar).Value = dwData["BSKNOTEN"].ToString();
            or1.Parameters.Add("BSFAKTOR", OracleType.NVarChar).Value = dwData["BSFAKTOR"].ToString();
            or1.Parameters.Add("BSSRFNR", OracleType.NVarChar).Value = dwData["BSSRFNR"].ToString();
            or1.Parameters.Add("BSCADKEY", OracleType.NVarChar).Value = dwData["BSCADKEY"].ToString();
            or1.Parameters.Add("BSANZTB", OracleType.NVarChar).Value = dwData["BSANZTB"].ToString();
            or1.Parameters.Add("BSKRANF", OracleType.NVarChar).Value = dwData["BSKRANF"].ToString();
            or1.Parameters.Add("BSKRENDE", OracleType.NVarChar).Value = dwData["BSKRENDE"].ToString();
            or1.Parameters.Add("BSKRMITTE", OracleType.NVarChar).Value = dwData["BSKRMITTE"].ToString();
            or1.Parameters.Add("BSFDFLGKR", OracleType.NVarChar).Value = dwData["BSFDFLGKR"].ToString();
            or1.Parameters.Add("BSKRART", OracleType.NVarChar).Value = dwData["BSKRART"].ToString();
            or1.Parameters.Add("BSSCHAERBR", OracleType.NVarChar).Value = dwData["BSSCHAERBR"].ToString();
            or1.Parameters.Add("BSRIETF", OracleType.NVarChar).Value = dwData["BSRIETF"].ToString();
            or1.Parameters.Add("BSFDFLGRI", OracleType.NVarChar).Value = dwData["BSFDFLGRI"].ToString();
            or1.Parameters.Add("BSBEM", OracleType.NVarChar).Value = dwData["BSBEM"].ToString();
            or1.Parameters.Add("BSZOLLTNR", OracleType.NVarChar).Value = dwData["BSZOLLTNR"].ToString();
            or1.Parameters.Add("BSSUBSTGR", OracleType.NVarChar).Value = dwData["BSSUBSTGR"].ToString();
            or1.Parameters.Add("BSHANDICAP", OracleType.NVarChar).Value = dwData["BSHANDICAP"].ToString();
            or1.Parameters.Add("BSAFFINITY", OracleType.NVarChar).Value = dwData["BSAFFINITY"].ToString();
            or1.Parameters.Add("BSSUBST1", OracleType.NVarChar).Value = dwData["BSSUBST1"].ToString();
            or1.Parameters.Add("BSSUBST2", OracleType.NVarChar).Value = dwData["BSSUBST2"].ToString();
            or1.Parameters.Add("BSBLEACHED", OracleType.NVarChar).Value = dwData["BSBLEACHED"].ToString();
            or1.Parameters.Add("BSCLORINEST", OracleType.NVarChar).Value = dwData["BSCLORINEST"].ToString();
            or1.Parameters.Add("BSFIXTEMP", OracleType.NVarChar).Value = dwData["BSFIXTEMP"].ToString();
            or1.Parameters.Add("BSPRODH", OracleType.NVarChar).Value = dwData["BSPRODH"].ToString();
            or1.Parameters.Add("BSMATKL", OracleType.NVarChar).Value = dwData["BSMATKL"].ToString();
            or1.Parameters.Add("BSPLGRP", OracleType.NVarChar).Value = dwData["BSPLGRP"].ToString();
            or1.Parameters.Add("BSPLANART", OracleType.NVarChar).Value = dwData["BSPLANART"].ToString();
            or1.Parameters.Add("BSEKGRP", OracleType.NVarChar).Value = dwData["BSEKGRP"].ToString();
            or1.Parameters.Add("BSBSTME", OracleType.NVarChar).Value = dwData["BSBSTME"].ToString();
            or1.Parameters.Add("BSVABME", OracleType.NVarChar).Value = dwData["BSVABME"].ToString();
            or1.Parameters.Add("BSEKWSL", OracleType.NVarChar).Value = dwData["BSEKWSL"].ToString();
            or1.Parameters.Add("BSINKZ", OracleType.NVarChar).Value = dwData["BSINKZ"].ToString();
            or1.Parameters.Add("BSINPR", OracleType.NVarChar).Value = dwData["BSINPR"].ToString();
            or1.Parameters.Add("BSBEWAB", OracleType.NVarChar).Value = dwData["BSBEWAB"].ToString();
            or1.Parameters.Add("BSFILLER", OracleType.NVarChar).Value = dwData["BSFILLER"].ToString();
            Connect2Proxy(or1);
        }
        public DataSet QueryBSSATZ(DataRow dwData, int option = 1)
        {
            DataSet setReturn = new DataSet();
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM BSSATZ WHERE  ROWNUM<2 ";
                    break;
                case 1:
                    or1.CommandText = "SELECT * FROM  BSSATZ"
                        + " WHERE "
                        + " BSMANDANT=:BSMANDANT AND BSKART=:BSKART";
                    or1.Parameters.Add("BSMANDANT", OracleType.NVarChar).Value = dwData["BSMANDANT"].ToString();
                    or1.Parameters.Add("BSKART", OracleType.NVarChar).Value = dwData["BSKART"].ToString(); 
                    break;
                case 2:
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "SELECT DISTINCT  WF.* FROM WDECK WD "
                        + " JOIN WASTKE ON WDMANDANT=WAKMANDANT AND WDART=WAKART AND WDVAR=WAKVAR AND WDFARBE=WAKWCOL"
                        + " JOIN BSSATZ WF ON BSMANDANT=WAKMANDANT AND BSKART=WAKKART"
                        + " WHERE WDMANDANT=:WDMANDANT AND WDSA='K' AND WDTYP='B' AND WDWAKZ='F' AND WDAUFKRS=:WDAUFKRS  AND WDAUFLFD=:WDAUFLFD";
                    or1.Parameters.Add("WDMANDANT", OracleType.NVarChar).Value = dwData["WDMANDANT"].ToString();
                    or1.Parameters.Add("WDAUFKRS", OracleType.NVarChar).Value = dwData["WDAUFKRS"].ToString();
                    or1.Parameters.Add("WDAUFLFD", OracleType.NVarChar).Value = dwData["WDAUFLFD"].ToString();
                    break;
                case 3:
                    or1.CommandText = "SELECT DISTINCT M.* FROM  BSSATZ M JOIN ABPOS ON ABPART=BSKART AND ABPMANDANT=BSMANDANT"
                        + " WHERE "
                        + " ABPMANDANT=:ABPMANDANT AND ABPAUFKRS=:ABPAUFKRS AND ABPAUFLFD=:ABPAUFLFD";
                    or1.Parameters.Add("ABPMANDANT", OracleType.NVarChar).Value = dwData["ABPMANDANT"].ToString();
                    or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwData["ABPAUFKRS"].ToString();
                    or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwData["ABPAUFLFD"].ToString();
                    break;
                default:
                    break;
            } 
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "BSSATZ";
            }
            setReturn.Merge(Result);
            return setReturn;
        }
    }
}
